'use strict';

const { addFollowerFailInfo, deleteFollowerFailInfo } = require('../utils/ErrorModel');
const { ErrorModel, SuccessModel } = require('../utils/resultModel');

module.exports = app => {
  class ProfileController extends app.Controller {

    async index() {
      // 在session中获取用户名
      const { userName } = this.ctx.session.userInfo;
      // 重定向
      await this.ctx.redirect(`/profile/${userName}`);
    }

    // 重定向个人主页
    async profileUser() {

      // 获取当前的用户名
      const { userName: curUserName } = this.ctx.params;
      // 获取登录人的用户名
      const { userName, id: userId } = this.ctx.session.userInfo;
      let isMe = true;
      let curUserInfo = {};
      if (curUserName !== userName) { // 如果不相等，说明查看的不是本人信息
        isMe = false;
        // 获取当前用户信息前，先判断这个用户是否存在
        const result = await this.service.user.getUserInfo(curUserName);
        if (result) {
          curUserInfo = result;
        } else {
          await this.ctx.render('404.html');
          return;
        }
      } else {
        curUserInfo = this.ctx.session.userInfo;
      }

      // 获取博客信息 service
      const result = await this.service.blog.getProfileBlogList({ userName: curUserName, pageSize: 5 });

      // 获取粉丝列表
      const { userList: fansList, count: fansCount } = await this.service.userRelation.getUsersByFollowerId(curUserInfo.id);

      // 获取关注列表
      const { userList: followList, count: followCount } = await this.service.userRelation.getFollower(curUserInfo.id);

      // 是否已经关注此人
      const isMeFollow = fansList.some(item => {
        return item.userName === userName;
      });

      // 获取 atme 的数量
      const atCount = await this.service.atRelation.getAtCount(userId);

      // 最终返回的数据包含 博客信息，用户基本信息
      await this.ctx.render('profile.html', {
        userData: {
          atCount,
          userInfo: curUserInfo,
          fansData: { // 粉丝
            count: fansCount,
            fansList,
          },
          followData: { // 关注
            count: followCount,
            followList,
          },
          isMe,
          isMeFollow,
        },
        blogList: result.blogList,
        pageIndex: 1, // 当前页码
        pageSize: 5, // 每页显示的数量
        count: result.count, // 微博的总数量
      });
    }

    // 关注
    async follow() {
      // 获取id session中获取本人id
      const { id: myUserId } = this.ctx.session.userInfo;
      // 获取followerId 前端传递过来的被关注人id  post方式提交的数据
      const { followerId } = this.ctx.request.body;

      try {
        // 调用 service
        const result = await this.service.userRelation.addFollower(myUserId, followerId);
        // 添加成功
        this.ctx.body = new SuccessModel(result);

      } catch (err) {
        // 添加关注失败
        this.ctx.body = new ErrorModel(addFollowerFailInfo);
      }

    }

    // 取消关注
    async unFollow() {

      const { id: myUserId } = this.ctx.session.userInfo;
      const { followerId } = this.ctx.request.body;

      const result = await this.service.userRelation.deleteFollower(myUserId, followerId);
      console.log(result);
      if (result) {
        this.ctx.body = new SuccessModel();
        return;
      }
      this.ctx.body = new ErrorModel(deleteFollowerFailInfo);
    }
  }
  return ProfileController;
};
